package com.gzt.bookcatalog.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class BookServiceLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(BookServiceLogAspect.class);

    /**
     * 定义切入点，匹配 BookService 类里的所有方法
     */
    @Pointcut("execution(* com.gzt.bookcatalog.service.BookService.*(..))")
    public void bookServiceMethods() {}

    /**
     * 前置通知，在目标方法执行前记录日志
     * @param joinPoint 连接点信息
     */
    @Before("bookServiceMethods()")
    public void beforeBookServiceMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.info("即将执行 BookService 的方法: {}，参数: {}", methodName, args);
    }

    /**
     * 后置返回通知，在目标方法正常返回后记录日志
     * @param joinPoint 连接点信息
     * @param result 方法返回结果
     */
    @AfterReturning(pointcut = "bookServiceMethods()", returning = "result")
    public void afterReturningBookServiceMethod(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        logger.info("BookService 的方法: {} 执行成功，返回结果: {}", methodName, result);
    }
}